package order

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/common/response"
	oreq "counter-help/admin/server/model/order/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type OrderAPi struct{}

// GetOrderList 获取订单列表
func (o *OrderAPi) GetOrderList(c *gin.Context) {
	var pageInfo oreq.OrderListSearch
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		global.GVA_LOG.Error("解析参数错误!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}

	list, total, err := OrderService.OrderList(&pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

func (o *OrderAPi) GetMatchedOrderList(c *gin.Context) {
	var pageInfo oreq.OrderListSearch
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		global.GVA_LOG.Error("解析参数错误!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}

	list, total, err := OrderService.MatchedOrderList(&pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// UpdateUnFinishOrder  更新未匹配成功订单
func (o *OrderAPi) UpdateUnFinishOrder(c *gin.Context) {
	var updateInfo oreq.UpdateUnFinishOrderReq
	err := c.ShouldBindJSON(&updateInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = OrderService.UpdateUnFinishOrder(&updateInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("修改成功！", c)
}

// UpdateFinishOrder 更新匹配成功订单
func (o *OrderAPi) UpdateFinishOrder(c *gin.Context) {
	var updateInfo oreq.UpdateFinishOrderResp
	err := c.ShouldBindJSON(&updateInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
	}
	err = OrderService.UpdateFinishOrder(&updateInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("修改成功！", c)
}
func (o *OrderAPi) AfterSalesOrder(c *gin.Context) {
	var updateInfo oreq.AfterSalesOrderResp
	err := c.ShouldBindJSON(&updateInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
	}
	err = OrderService.AfterSalesOrder(&updateInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("修改成功！", c)
}
func (o *OrderAPi) CreateAfterSales(c *gin.Context) {
	var updateInfo oreq.CreateAfterSalesOrderResp
	err := c.ShouldBindJSON(&updateInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
	}
	err = OrderService.CreateSalesOrder(&updateInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("修改成功！", c)
}

// AddPrice 增款
func (o *OrderAPi) AddPrice(c *gin.Context) {
	var priceInfo oreq.AddPriceReq
	err := c.ShouldBindJSON(&priceInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
	}
	err = OrderService.AddPrice(&priceInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
	}
	response.OkWithMessage("增款成功！", c)
}

// SubPrice 减款
func (o *OrderAPi) SubPrice(c *gin.Context) {
	var priceInfo oreq.SubPriceReq
	err := c.ShouldBindJSON(&priceInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
	}
	err = OrderService.SubPrice(&priceInfo)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
	}
	response.OkWithMessage("减款成功！", c)
}
